package com.gjintel.framework.projecttype.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.framework.log.util.LogUtil;
import com.gjintel.framework.log.util.LogUtil.LogType;
import com.gjintel.framework.projecttype.model.ProjectType;
import com.gjintel.framework.projecttype.service.IProjectTypeService;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.util.StringUtil;

@RequestMapping("/business/projecttype")
@Controller
public class ProjectTypeController {

	private static final Logger LOG = Logger
			.getLogger(ProjectTypeController.class);

	@Autowired
	private IProjectTypeService projectTypeService;



	/**
	 * @description 获得输入数据信息
	 * @author cxj
	 */
	@RequestMapping("/input")
	public ModelAndView input(String id) {
		ModelAndView mv = new ModelAndView(
				"/business/projecttype/project-type-input");
		if (!StringUtil.isEmpty(id)) {
			ProjectType projectType = projectTypeService.get(ProjectType.class, id);
			mv.addObject("projectType", projectType);
		}
		   return mv;
	}

	/**
	 * @description 得的所有的记录数据
	 * @author cxj
	 */
	@RequestMapping("/listAll")
	@ResponseBody
	public JSONArray listAll() {
		List<ProjectType> list = this.projectTypeService.findAll();
		return JSONArray.fromObject(list);
	}

	/**
	 * @description 分页查询数据
	 * @author cxj
	 */
	@RequestMapping("/list")
	@SuppressWarnings("unchecked")
	@ResponseBody
	public JSONObject list(Page page, ProjectType projectType) {
		this.projectTypeService.findAll(page, projectType);
		return CommUtil.getPageJSONData(page, true);
	}


	/**
	 * @description 保存数据
	 * @author cxj
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(HttpSession session, ProjectType projectType) {
		User user = (User) session.getAttribute("loginUser");
		if (projectType != null) {
			boolean sign = false;
			if (StringUtil.isEmpty(projectType.getId())) {
				projectType.setId(null);
				projectType.setCreateTime(new Date());
				sign = true;
				List<ProjectType> projectTypes=this.projectTypeService.findAll();
				for (ProjectType projType : projectTypes) {
					//项目类别名称不能相同
					if (projType.getName().equals(projectType.getName())) {
						return Constant.SAVE_UN_SUCCESS;
					}
				}
			}
			projectType.setUpdateTime(new Date());
			this.projectTypeService.saveOrUpdate(projectType);
			if (sign) {
				LogUtil
						.addLog(new LogInfo("添加项目类型", user.getId(), user
								.getName(), String.valueOf(LogType.ADD
								.ordinal()), projectType.getClass()
								.getSimpleName(), projectType.getId()));
			} else {
				LogUtil
						.addLog(new LogInfo("更新项目类型", user.getId(), user
								.getName(), String.valueOf(LogType.UPDATE
								.ordinal()), projectType.getClass()
								.getSimpleName(), projectType.getId()));
			}
			return Constant.SAVE_SUCCESS;
		}
		return Constant.SAVE_UN_SUCCESS;
	}

	
	/**
	 * @description 删除数据
	 * @author cxj
	 */
	@RequestMapping("/remove")
	@ResponseBody
	public String remove(HttpSession session, String deleteIds)
			throws Exception {
		this.projectTypeService.removeData(deleteIds);
		User user = (User) session.getAttribute("loginUser");
		LogUtil.addLog(new LogInfo("删除项目类型", user.getId(), user.getName(),
				String.valueOf(LogType.DELETE.ordinal()), ProjectType.class
						.getSimpleName(), deleteIds));
		WebApplicationContext webApplicationContext = ContextLoader
				.getCurrentWebApplicationContext();
		ServletContext servletContext = webApplicationContext
				.getServletContext();
		return Constant.DEL_SUCCESS;
	}

}
